using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using au.Swindles.LoanDataService.ObjectLayer;
using System.Collections.Generic;
using au.Swindles.LoanDataService.AdapterLayer;

namespace au.Swindles.LoanDataService.BLLayer
{
    public static class BLApplication
    {


        public static Application getLoanApplication(int pApplicationID)
        {
            Application app = null;
            try
            {
                List<Application> applicationlist = ApplicationAdapter.getLoanApplicationList();

                if (applicationlist != null)
                {
                    foreach (Application obj in applicationlist)
                    {
                        if (obj.ApplicationID == pApplicationID)
                        {
                            app = obj;
                            break;
                        }
                    }
                }
            }

            catch (Exception)
            {
            }
            return app;
        }

        public static List<Application> getCustomerLoanApplication(int pCustomerID)
        {
            List<Application> customerApplicationList = new List<Application>();
            try
            {
                List<Application> applicationlist = ApplicationAdapter.getLoanApplicationList();

                if (applicationlist != null)
                {
                    foreach (Application obj in applicationlist)
                    {
                        if (obj.CustomerID == pCustomerID)
                        {
                            customerApplicationList.Add(obj);
                        }
                    }
                }
            }

            catch (Exception)
            {
            }
            return customerApplicationList;
        }

        public static List<Application> getPendingLoanApproval(string pApplicationStatus)
        {
            List<Application> pendingApplicationList = new List<Application>();
            try
            {
                List<Application> applicationlist = ApplicationAdapter.getLoanApplicationList();

                if (applicationlist != null)
                {
                    foreach (Application obj in applicationlist)
                    {
                        if (string.Compare(obj.ApplicationStatus,pApplicationStatus,true) == 0)
                        {
                            pendingApplicationList.Add(obj);
                        }
                    }
                }
            }

            catch (Exception)
            {
            }
            return pendingApplicationList;
        }

        public static Application createLoanApplication(string pQuoteNumber, string pCustomerNumber, int pApplicationID, decimal pAssetValue, decimal pannualIncome, decimal pAnnualExpense, string pApplicationStatus, DateTime pStatusUpdateDate, int pStatusUpdateUserID, string pComments)
        {
            Application application = null;
            try
            {
                int quoteID = QuoteAdapter.getQuoteByQuoteNumber(pQuoteNumber);
                int customerID = QuoteAdapter.getCustomerByCustomerNumber(pCustomerNumber);


                if (0 == quoteID)
                    throw new Exception("Quote Number doesnt exist. Cannot create Application");
                else
                {
                    application = new Application();
                    application.CustomerID = customerID;
                    application.ApplicationID = pApplicationID;
                    application.QuoteID = quoteID;
                    application.AssetValue = pAssetValue;
                    application.AnnualIncome = pannualIncome;
                    application.AnnualExpense = pAnnualExpense;
                    application.ApplicationStatus = pApplicationStatus;
                    application.StatusUpdateDate = pStatusUpdateDate;
                    application.StatusUpdateUserID = pStatusUpdateUserID;
                    application.Comments = pComments;
                    application = ApplicationAdapter.createApplication(application);
                }

            }
            catch (Exception)
            {
            }
            return application;
        }


        public static bool updateLoanApplication(Application pApplication)
        {
            try
            {
                ApplicationAdapter.updateLoanApplication(pApplication);
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
    }
}